<template>
  <div class="home-container">
    <!-- 轮播图 -->
    <div class="carousel-section">
      <el-carousel height="400px" indicator-position="outside">
        <el-carousel-item v-for="(banner, index) in bannerList" :key="banner.bannerId">
          <a
              v-if="banner.linkUrl"
              :href="banner.linkUrl"
              :target="isExternalLink(banner.linkUrl) ? '_blank' : '_self'"
              class="carousel-link"
          >
            <div class="carousel-item">
              <img
                  :src="banner.imageUrl"
                  :alt="banner.title" style="width: 100%; height: 100%; object-fit: cover;"
              >
            </div>
          </a>
          <div v-else class="carousel-item">
            <img
                :src="banner.imageUrl"
                :alt="banner.title" style="width: 100%; height: 100%; object-fit: cover;"
            >
          </div>
        </el-carousel-item>
      </el-carousel>
    </div>

    <!-- 功能卡片 -->
    <div class="function-cards">
      <el-card
          v-for="(card, index) in cards"
          :key="index"
          :class="['card-item', `card-${index + 1}`]"
          shadow="hover"
      >
        <template #header>
          <div class="card-header">
            <el-icon :size="24" class="card-icon">
              <component :is="card.icon"/>
            </el-icon>
            <span>{{ card.title }}</span>
          </div>
        </template>
        <div class="card-content">
          <el-button type="primary" plain @click="card.onClick">{{ card.buttonText }}</el-button>
        </div>
      </el-card>
    </div>
    <!-- 信息展示区 -->
    <div class="info-container">
      <!-- 左侧通知公告 -->
      <div class="left-side">
        <!-- 公告板 -->
        <el-card class="notice-board" shadow="hover">
          <template #header>
            <div class="board-header">
              <el-icon>
                <Bell/>
              </el-icon>
              <span>最新公告</span>
            </div>
          </template>
          <ul class="notice-list">
            <li v-for="(notice,index) in noticeList" :key="index" class="notice-item">
              <span class="notice-title" @click="toNoticeDetails(notice.noticeId)">{{ notice.title }}</span>
              <span class="notice-time">{{ notice.createTime }}</span>
            </li>
          </ul>
        </el-card>

        <!-- 行业新闻 -->
        <el-card class="news-board" shadow="hover">
          <template #header>
            <div class="board-header">
              <el-icon>
                <Reading/>
              </el-icon>
              <span>心理快讯</span>
            </div>
          </template>
          <div class="news-list">
            <div v-for="(news,index) in newsList" :key="index" class="news-item">
              <div class="news-content" @click="toNewsDetails(news.newsId)">
                <h4 class="news-title">{{ news.title }}</h4>
                <p class="news-desc">{{ news.summary }}</p>
              </div>
              <img :src="news.coverImage" class="news-cover">
            </div>
          </div>
        </el-card>
      </div>

      <!-- 右侧专业文章 -->
      <div class="right-side">
        <el-card class="article-board" shadow="hover">
          <template #header>
            <div class="board-header">
              <el-icon>
                <Notebook/>
              </el-icon>
              <span>精选文章</span>
            </div>
          </template>
          <div class="article-list">
            <div v-for="(article,index) in articleList" :key="article.articleId" class="article-item">
              <div class="article-cover">
                <img :src="article.coverImage">
              </div>
              <div class="article-content">
                <h3 class="article-title" @click="toArticleDetails(article.articleId)">{{ article.title }}</h3>
                <p class="article-excerpt">{{ article.description }}</p>
                <div class="article-meta">
                  <span class="read-count">👁 {{ article.viewCount }}</span>
                  <span class="publish-time">{{ article.publishTime }}</span>
                </div>
              </div>
            </div>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import {getCurrentInstance, onMounted, ref} from 'vue'
import {Calendar, Collection, Document, Notebook} from '@element-plus/icons-vue'
import useUserStore from '@/store/modules/user';
import Cookies from "js-cookie";
import {listBanner} from "@/api/banner/banner.js";
import {getIndexArticle} from "@/api/articles/articles.js";
import {getIndexNotice} from "@/api/notice/notice.js";
import {getIndexNews} from "@/api/news/news.js";

const userStore = useUserStore();

const {proxy} = getCurrentInstance();

const articleList = ref([])

// 示例数据
const noticeList = ref([]);

const newsList = ref([]);


const cards = ref([
  {
    title: '待完成测评',
    value: 1,
    icon: Document,
    buttonText: '立即测评',
    onClick: () => proxy.$router.push('/online-assessments'),
  },
  {
    title: '已完成测评',
    value: 2,
    icon: Notebook,
    buttonText: '查看报告',
    onClick: () => proxy.$router.push('/history'),
  },
  {
    title: '在线预约',
    value: 3,
    icon: Calendar,
    buttonText: '立即预约',
    onClick: () => proxy.$router.push('/appointment'),
  },
  {
    title: '预约记录',
    value: 4,
    icon: Collection,
    buttonText: '查看记录',
    onClick: () => proxy.$router.push('/appointment-list'),
  }
])
const bannerList = ref([]);

//  是否外部链接
const isExternalLink = (url) => {
  return /^(http|https):\/\//.test(url);
};

// 加载轮播图
async function getBannerList() {
  try {
    const res = await listBanner();
    bannerList.value = res.rows;
  } catch (e) {
    console.log("获取轮播图列表失败:", e)
  }
}

// 加载首页文章
async function listIndexArticleList() {
  try {
    const res = await getIndexArticle();
    articleList.value = res.rows;
  } catch (e) {
    console.log("获取首页文章失败:", e)
  }
}

// 跳转文章详情
function toArticleDetails(articleId) {
  console.log("跳转详情页", articleId)
  proxy.$router.push({
    path: '/articleDetail',
    query: {  // 使用 query 传参
      articleId: articleId,
    }
  })
}

function toNewsDetails(newsId) {
  proxy.$router.push({
    path: '/newsDetail',
    query: {  // 使用 query 传参
      newsId: newsId,
    }
  })
}

// 跳转公告详情
function toNoticeDetails(noticeId) {
  console.log("跳转详情页", noticeId)
  proxy.$router.push({
    path: '/noticeDetail',
    query: {  // 使用 query 传参
      noticeId: noticeId,
    }
  })
}

// 加载首页公告
async function listIndexNoticeList() {
  getIndexNotice().then(res => {
    noticeList.value = res.rows;
  })
}

async function listIndexNewsList() {
  getIndexNews().then(res => {
    newsList.value = res.rows;
  })
}

onMounted(() => {
  getBannerList();
  listIndexArticleList();
  listIndexNoticeList();
  listIndexNewsList();
})
</script>

<style scoped>
.home-container {
  background-color: #f0f8ff;
  min-height: 100vh;
  padding: 20px;
}

.info-container {
  max-width: 1200px;
  margin: 30px auto;
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 30px;
}

.left-side {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.right-side {
  background: white;
  border-radius: 8px;
}

/* 公告板样式 */
.notice-board {
  transition: transform 0.3s;
}

.notice-board:hover {
  transform: translateY(-3px);
}

.notice-list {
  list-style: none;
  padding: 0;
}

.notice-item {
  display: flex;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid #eee;
  transition: background 0.3s;
}

.notice-item:hover {
  background: #f8f9fa;
}

.notice-title {
  color: #409EFF;
  cursor: pointer;
}

.notice-time {
  color: #999;
  font-size: 0.9em;
}

/* 新闻板样式 */
.news-item {
  display: flex;
  gap: 15px;
  padding: 15px 0;
  border-bottom: 1px solid #eee;
}

.news-cover {
  width: 100px;
  height: 70px;
  border-radius: 4px;
  object-fit: cover;
}

.news-title {
  color: #333;
  margin: 0 0 8px 0;
  cursor: pointer;
}

.news-desc {
  color: #666;
  font-size: 0.9em;
  line-height: 1.5;
}

/* 文章板样式 */
.article-item {
  display: grid;
  grid-template-columns: 200px 1fr;
  gap: 20px;
  padding: 20px 0;
  border-bottom: 1px solid #eee;
}

.article-cover img {
  width: 100%;
  height: 120px;
  border-radius: 6px;
  object-fit: cover;
}

.article-title {
  color: #2c3e50;
  margin: 0 0 10px 0;
  cursor: pointer;
  transition: color 0.3s;
}

.article-title:hover {
  color: #409EFF;
}

.article-excerpt {
  color: #7f8c8d;
  line-height: 1.6;
  margin-bottom: 10px;
}

.article-meta {
  display: flex;
  justify-content: space-between;
  color: #95a5a6;
  font-size: 0.9em;
}

.board-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  color: #409EFF;
}

.carousel-link:hover {
  cursor: pointer;
  opacity: 0.9;
  transition: opacity 0.3s ease;
}

.home-container {
  background-color: #f0f8ff;
  min-height: 100vh;
}


.auth-buttons .divider {
  color: #ddd;
  margin: 0 10px;
}

/* 导航菜单居中 */
.main-nav ::v-deep .el-menu {
  display: flex;
  justify-content: center;
}

.main-nav ::v-deep .el-menu-item,
.main-nav ::v-deep .el-sub-menu {
  flex-shrink: 0;
}

.carousel-section {
  max-width: 1200px;
  margin: 20px auto;
  border-radius: 8px;
  overflow: hidden;
}

.carousel-item {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  font-size: 24px;
}

.function-cards {
  max-width: 1200px;
  margin: 30px auto;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
  padding: 0 20px;
}

.card-item {
  transition: transform 0.3s;
}

.card-item:hover {
  transform: translateY(-5px);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 18px;
  font-weight: 500;
}

.card-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 15px;
  padding: 20px 0;
}

.number {
  font-size: 32px;
  font-weight: bold;
  color: #409EFF;
}

.card-1 {
  border-top: 4px solid #409EFF;
}

.card-2 {
  border-top: 4px solid #67C23A;
}

.card-3 {
  border-top: 4px solid #E6A23C;
}

.card-4 {
  border-top: 4px solid #F56C6C;
}
</style>
